<?php
include_once ("/includes/SimpleXmlExtended.class.php");

class Latex {
	private $documentclass;
	private $usepackage;
	private $configuracion;
	private $documento = Array (
		'inicio' => '',
		'fin' => ''
	);
	private $tabla = Array (
		'iniciotabla' => '',
		'filanombre' => Array (
			'multicolumn' => '',
			'texto' => '',
			'finfila' => ''
		),
		'filaambito' => Array (
			'multicolumn' => '',
			'texto' => '',
			'finfila' => ''
		),
		'filadefinicion' => Array (
			'multicolumn' => '',
			'texto' => '',
			'finfila' => ''
		),
		'filaacepcionvenezuela' => Array (
			'pais' => '',
			'divcolum' => '',
			'texto' => '',
			'finfila' => ''
		),
		'filaejemplovenezuela' => Array (
			'multicolumn' => '',
			'texto' => '',
			'finfila' => ''
		),
		'filafuentevenezuela' => Array (
			'multicolumn' => '',
			'texto' => '',
			'finfila' => ''
		),
		'filaacepcioncolombia' => Array (
			'pais' => '',
			'divcolum' => '',
			'texto' => '',
			'finfila' => ''
		),
		'filaejemplocolombia' => Array (
			'multicolumn' => '',
			'texto' => '',
			'finfila' => ''
		),
		'filafuentecolombia' => Array (
			'multicolumn' => '',
			'texto' => '',
			'finfila' => ''
		),
		'filaacepcionecuador' => Array (
			'pais' => '',
			'divcolum' => '',
			'texto' => '',
			'finfila' => ''
		),
		'filaejemploecuador' => Array (
			'multicolumn' => '',
			'texto' => '',
			'finfila' => ''
		),
		'filafuenteecuador' => Array (
			'multicolumn' => '',
			'texto' => '',
			'finfila' => ''
		),
		'filaacepcionperu' => Array (
			'pais' => '',
			'divcolum' => '',
			'texto' => '',
			'finfila' => ''
		),
		'filaejemploperu' => Array (
			'multicolumn' => '',
			'texto' => '',
			'finfila' => ''
		),
		'filafuenteperu' => Array (
			'multicolumn' => '',
			'texto' => '',
			'finfila' => ''
		),
		'fintabla' => ''
	);
	private $rutaXML = "datos/latex.xml";
	private $xml;

	# CONSTRUCTOR

	public function __construct() {
		$this->cargarXML();
	}

	#CARGAMOS XML  

	private function cargarXML() {
		$this->xml = simplexml_load_file($this->rutaXML);
	}

	#DEFINIMOS LOS GETS

	public function getDocumentclass() {
		return $this->documentclass;
	}

	public function getUsepackage() {
		return $this->usepackage;
	}

	public function getConfiguracion() {
		return $this->configuracion;
	}

	public function getDocumento() {
		return $this->documento;
	}

	public function getImagen() {
		return $this->imagen;
	}

	public function getTabla() {
		return $this->tabla;
	}

	#DEFINIMOS LOS SETS

	public function setDocumentclass($documentclass) {
		$this->documentclass = $documentclass;
	}

	public function setUsepackage($usepackage) {
		$this->usepackage = $usepackage;
	}

	public function setConfiguracion($configuracion) {
		$this->configuracion = $configuracion;
	}

	public function setDocumento($documento) {
		$this->documento = $documento;
	}

	public function setImagen($imagen) {
		$this->imagen = $imagen;
	}

	public function setTabla($tabla) {
		$this->tabla = $tabla;
	}

	public function extraerLatex() {
		$resul = $this->xml->xpath('/*');
		$this->getLatex($resul);
	}

	private function getLatex(Array $latexXMLlist) {
		$latexXML2 = $latexXMLlist[0];
		$this->documentclass = (string) $latexXML2->documentclass;
		$this->usepackage = (string) $latexXML2->usepackage;
		$this->configuracion = (string) $latexXML2->configuracion;
		$latexXMLl = $latexXML2->contenido;
		$latexXML = $latexXMLl[0];
		$this->documento['inicio'] = (string) $latexXML->inicio;
		$this->documento['fin'] = (string) $latexXML->fin;
		$this->tabla['iniciotabla'] = (string) $latexXML->tabla->iniciotabla;
		$filal = $latexXML->tabla[0]->filanombre;
		$fila = $filal[0];
		$this->tabla['filanombre']['multicolumn'] = (string) $fila->multicolumn;
		$this->tabla['filanombre']['texto'] = (string) $fila->texto;
		$this->tabla['filanombre']['finfila'] = (string) $fila->finfila;

		$filal = $latexXML->tabla[0]->filaambito;
		$fila = $filal[0];
		$this->tabla['filaambito']['multicolumn'] = (string) $fila->multicolumn;
		$this->tabla['filaambito']['texto'] = (string) $fila->texto;
		$this->tabla['filaambito']['finfila'] = (string) $fila->finfila;

		$filal = $latexXML->tabla[0]->filadefinicion;
		$fila = $filal[0];
		$this->tabla['filadefinicion']['multicolumn'] = (string) $fila->multicolumn;
		$this->tabla['filadefinicion']['texto'] = (string) $fila->texto;
		$this->tabla['filadefinicion']['finfila'] = (string) $fila->finfila;
		$filal = $latexXML->tabla[0]->filaacepcionvenezuela;
		$fila = $filal[0];
		$this->tabla['filaacepcionvenezuela']['pais'] = (string) $fila->pais;
		$this->tabla['filaacepcionvenezuela']['divcolum'] = (string) $fila->divcolum;
		$this->tabla['filaacepcionvenezuela']['texto'] = (string) $fila->texto;
		$this->tabla['filaacepcionvenezuela']['finfila'] = (string) $fila->finfila;
		$filal = $latexXML->tabla[0]->filaejemplovenezuela;
		$fila = $filal[0];
		$this->tabla['filaejemplovenezuela']['multicolumn'] = (string) $fila->multicolumn;
		$this->tabla['filaejemplovenezuela']['texto'] = (string) $fila->texto;
		$this->tabla['filaejemplovenezuela']['finfila'] = (string) $fila->finfila;
		$filal = $latexXML->tabla[0]->filafuentevenezuela;
		$fila = $filal[0];
		$this->tabla['filafuentevenezuela']['multicolumn'] = (string) $fila->multicolumn;
		$this->tabla['filafuentevenezuela']['texto'] = (string) $fila->texto;
		$this->tabla['filafuentevenezuela']['finfila'] = (string) $fila->finfila;
		$filal = $latexXML->tabla[0]->filaacepcioncolombia;
		$fila = $filal[0];
		$this->tabla['filaacepcioncolombia']['pais'] = (string) $fila->pais;
		$this->tabla['filaacepcioncolombia']['divcolum'] = (string) $fila->divcolum;
		$this->tabla['filaacepcioncolombia']['texto'] = (string) $fila->texto;
		$this->tabla['filaacepcioncolombia']['finfila'] = (string) $fila->finfila;
		$filal = $latexXML->tabla[0]->filaejemplocolombia;
		$fila = $filal[0];
		$this->tabla['filaejemplocolombia']['multicolumn'] = (string) $fila->multicolumn;
		$this->tabla['filaejemplocolombia']['texto'] = (string) $fila->texto;
		$this->tabla['filaejemplocolombia']['finfila'] = (string) $fila->finfila;
		$filal = $latexXML->tabla[0]->filafuentecolombia;
		$fila = $filal[0];
		$this->tabla['filafuentecolombia']['multicolumn'] = (string) $fila->multicolumn;
		$this->tabla['filafuentecolombia']['texto'] = (string) $fila->texto;
		$this->tabla['filafuentecolombia']['finfila'] = (string) $fila->finfila;
		$filal = $latexXML->tabla[0]->filaacepcionecuador;
		$fila = $filal[0];
		$this->tabla['filaacepcionecuador']['pais'] = (string) $fila->pais;
		$this->tabla['filaacepcionecuador']['divcolum'] = (string) $fila->divcolum;
		$this->tabla['filaacepcionecuador']['texto'] = (string) $fila->texto;
		$this->tabla['filaacepcionecuador']['finfila'] = (string) $fila->finfila;
		$filal = $latexXML->tabla[0]->filaejemploecuador;
		$fila = $filal[0];
		$this->tabla['filaejemploecuador']['multicolumn'] = (string) $fila->multicolumn;
		$this->tabla['filaejemploecuador']['texto'] = (string) $fila->texto;
		$this->tabla['filaejemploecuador']['finfila'] = (string) $fila->finfila;
		$filal = $latexXML->tabla[0]->filafuenteecuador;
		$fila = $filal[0];
		$this->tabla['filafuenteecuador']['multicolumn'] = (string) $fila->multicolumn;
		$this->tabla['filafuenteecuador']['texto'] = (string) $fila->texto;
		$this->tabla['filafuenteecuador']['finfila'] = (string) $fila->finfila;
		$filal = $latexXML->tabla[0]->filaacepcionperu;
		$fila = $filal[0];
		$this->tabla['filaacepcionperu']['pais'] = (string) $fila->pais;
		$this->tabla['filaacepcionperu']['divcolum'] = (string) $fila->divcolum;
		$this->tabla['filaacepcionperu']['texto'] = (string) $fila->texto;
		$this->tabla['filaacepcionperu']['finfila'] = (string) $fila->finfila;
		$filal = $latexXML->tabla[0]->filaejemploperu;
		$fila = $filal[0];
		$this->tabla['filaejemploperu']['multicolumn'] = (string) $fila->multicolumn;
		$this->tabla['filaejemploperu']['texto'] = (string) $fila->texto;
		$this->tabla['filaejemploperu']['finfila'] = (string) $fila->finfila;
		$filal = $latexXML->tabla[0]->filafuenteperu;
		$fila = $filal[0];
		$this->tabla['filafuenteperu']['multicolumn'] = (string) $fila->multicolumn;
		$this->tabla['filafuenteperu']['texto'] = (string) $fila->texto;
		$this->tabla['filafuenteperu']['finfila'] = (string) $fila->finfila;
		$this->tabla['fintabla'] = (string) $latexXML->tabla[0]->fintabla;

	}

	public function llenarLatex($dicionario) {
		$dicionario['nombre'] = $this->caracteresLatex($dicionario['nombre']);
		$this->tabla['filanombre']['texto'] = "{\\\bf\\\em " . $dicionario['nombre'] . "}";
		foreach ($dicionario['ambito'] as $clave => $valor) {
			$valor = $this->caracteresLatex($valor);
			$this->tabla['filaambito']['texto'] = $this->tabla['filaambito']['texto'] . ", " . $valor;
		}
		$this->tabla['filaambito']['texto'] = substr($this->tabla['filaambito']['texto'], 2);
		$this->tabla['filaambito']['texto'] = "{" . $this->tabla['filaambito']['texto'] . "}";
		$dicionario['categoria'] = $this->caracteresLatex($dicionario['categoria']);
		$dicionario['definicion'] = $this->caracteresLatex($dicionario['definicion']);
		$this->tabla['filadefinicion']['texto'] = "{" . "{\\\em " . $dicionario['categoria'] . "}. " . $dicionario['definicion'] . "}";
		$paises = Array (
			"venezuela",
			"colombia",
			"ecuador",
			"peru"
		);
		foreach ($paises as $valor) {
			$dicionario[$valor]['acepcion'] = $this->caracteresLatex($dicionario[$valor]['acepcion']);
			$dicionario[$valor]['ejemplo'] = $this->caracteresLatex($dicionario[$valor]['ejemplo']);
			$dicionario[$valor]['fuente'] = $this->caracteresLatex($dicionario[$valor]['fuente']);
			$this->tabla['filaacepcion' . $valor]['texto'] = "{" . $dicionario[$valor]['acepcion'] . "}";
			$this->tabla['filaejemplo' . $valor]['texto'] = "{" . $dicionario[$valor]['ejemplo'] . "}";
			$this->tabla['filafuente' . $valor]['texto'] = "{" . $dicionario[$valor]['fuente'] . "}";
		}

	}

	private function caracteresLatex($cadena) {
		$cadena = preg_replace('/%/', '\\\\\%', $cadena);
		$cadena = preg_replace('/{/', '\\\\\{', $cadena);
		$cadena = preg_replace('/}/', '\\\\\}', $cadena);
		$cadena = preg_replace('/~/', '$\\\\\~$', $cadena);
		$cadena = preg_replace('/\$/', '$\\\\\\\$$', $cadena);
		return $cadena;
	}

}
?>
